
package cup.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cup.common.utils.GeneratorMapByList;
import cup.core.launch.constant.AppConstant;
import cup.core.mp.support.Condition;
import cup.core.mp.support.Query;
import cup.core.tenant.annotation.NonDS;
import cup.core.tool.api.R;
import cup.core.tool.utils.Func;
import cup.modules.system.dto.DeptUserDTO;
import cup.modules.system.entity.*;
import cup.modules.system.service.*;
import cup.modules.system.vo.*;
import cup.modules.system.wrapper.PostWrapper;
import cup.modules.system.wrapper.UserWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liquibase.pro.packaged.D;
import liquibase.pro.packaged.U;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 查询控制器
 *
 * @author
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/search")
@Api(value = "z查询", tags = "查询")
public class SearchController {

	private final IRoleService roleService;

	private final IDeptService deptService;

	private final IPostService postService;

	private final IUserService userService;

	private final IRegionService regionService;


	private static String ALIAS = "city-system";

	/**
	 * 角色信息查询
	 */
	@GetMapping("/role")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "角色信息查询", notes = "传入roleName或者parentId")
	public R<List<RoleVO>> roleSearch(String roleName, Long parentId) {
		return R.data(roleService.search(roleName, parentId));
	}

	/**
	 * 部门信息查询
	 */
	@GetMapping("/dept")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "部门信息查询", notes = "传入deptName或者parentId")
	public R<List<DeptVO>> deptSearch(String deptName, Long parentId) {
		return R.data(deptService.search(deptName, parentId));
	}

	/**
	 * 岗位信息查询
	 */
	@GetMapping("/post")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "岗位信息查询", notes = "传入postName")
	public R<IPage<PostVO>> postSearch(String postName, Query query) {
		LambdaQueryWrapper<Post> queryWrapper = Wrappers.<Post>query().lambda();
		if (Func.isNotBlank(postName)) {
			queryWrapper.like(Post::getPostName, postName);
		}
		IPage<Post> pages = postService.page(Condition.getPage(query), queryWrapper);
		return R.data(PostWrapper.build().pageVO(pages));
	}

	@GetMapping("/getUserList")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "根据部门(区划)id获取用户列表")
	public R<List<DeptUserDTO>> getUserList(String deptIds) {
//		List<DeptUserDTO> result = new ArrayList<>();
//		List<Long> ids = Func.toLongList(deptIds);
//		Map<Long, User> generator = GeneratorMapByList.generator(() -> {
//			return userService.findByRoleId(deptIds);
//		}, "id", Long.class);
//		for (Long deptId : ids) {
//			DeptUserDTO dto = new DeptUserDTO();
//			dto.setDeptId(deptId);
//			List<User> users = new ArrayList<>();
//			for (Long id : generator.keySet()) {
//				if (generator.get(id).getDeptId().equals(deptIds)){
//					users.add(generator.get(id));
//				}
//			}
//			dto.setUsers(users);
//			result.add(dto);
//		}

		List<User> userList = userService.getBaseMapper().selectList(
			new QueryWrapper<User>()
				.in("dept_id", Func.toStrList(deptIds)));
		Map<Long, List<User>> map = new HashMap<>();
		for (User user : userList) {
			Long aLong = Long.valueOf(user.getDeptId());
			if (map.containsKey(aLong)) {
				map.get(aLong).add(user);
			} else {
				List<User> list=new ArrayList<>();
				list.add(user);
				map.put(aLong, list);
			}
		}

		Role role = roleService.getOne(new QueryWrapper<Role>().eq("role_alias", ALIAS));

		List<DeptUserDTO> result = new ArrayList<>();
		for (Long id : Func.toLongList(deptIds)) {
			DeptUserDTO dto = new DeptUserDTO();
			dto.setDeptId(id);
			List<UserVO> users = new ArrayList<>();
			if (map.containsKey(id)) {
				for (User user : map.get(id)) {
					if (!user.getRoleId().contains(role.getId().toString())) {
						users.add(UserWrapper.build().entityVO(user));
					}
				}
			}
			//dto.setUsers(userService.getBaseMapper().selectList(new QueryWrapper<User>().eq("dept_id",String.valueOf(id))));
			dto.setUsers(users);
			result.add(dto);
		}
		return R.data(result);
	}

	@GetMapping("/getUserById")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "根据用户id获取数据")
	public R<UserResultVO> getUserById(@RequestParam(required = true) Long id) {
		User user = userService.userInfo(id).getUser();
		UserResultVO vo = new UserResultVO();
		vo.setId(id);
		vo.setName(user.getName());

		Dept dept = deptService.getById(user.getDeptId());
		String regionId = dept.getRegionId();
		Region region = regionService.getOne(new QueryWrapper<Region>().eq("code", regionId));
		vo.setPartitionCode(region.getCityCode());
		vo.setPartitionName(region.getCityName());
		vo.setRegionCode(region.getDistrictCode());
		vo.setRegionName(region.getDistrictName());
		return R.data(vo);
	}

}
